<?php
class actAction extends WapAction {
    protected $layout = 'act';
    
    public function execute()
    {
        if (empty($_SESSION['admin']) && $this->params['action'] != 'login') {
            $this->redirect('act/login');
        }
        $this->view->setLayoutPath(ROOT.'/theme/act/layout');
        parent::execute();
    }
    
    public function doLogins()
    {
    	//$this->view->layout = '';
    	
    	if(isset($_SESSION['admin']))
    	{
    		//$this->redirect(url_for('admin/index/'));
    	}
        if(!empty($_POST)){
            $username = $_POST['username'];
            $password = AdminHelper::passwordEncode($_POST['password']);
            
            $conditions	= array('username'=>$username, 'password'=>$password);
            
            $user	= ORM_Finder::findOne('Admin', "username='$username' and password='$password'", '*');

            if(!empty($user)){
            	$_SESSION['admin']	 		= $user->getUsername();
            	$_SESSION['adminid'] 		= $user->getID();
            	$_SESSION['ids']			= $user->getIds();
            	$_SESSION['daohang']		= AdminHelper::getAdminDaohang();
            	Common::Msg(date('Y年m月d日H时i分').'\n欢迎您:'.$_SESSION['admin'], url_for('admin/index/'));
            }else{
            	Common::Msg('登陆失败', url_for('admin/login/'));
            }
        }
        $this->view->layout = '';
        $this->render('login');
    }
    
    public function doLogout() {
        session_destroy();
        $this->redirect('/');
    }
    
    public function doIndex() {
    	$this->view->layout = '';
        $this->setPageTitle('欢迎');
        $this->set('daohang', AdminHelper::getAdminDaohang());
       // print_r(AdminHelper::getAdminDaohang());
        $this->render();
    }
    
    /**
     * 修改密码
     */
    public function doAdminPwd()
    {
    	if(!empty($_POST))
    	{
    		$old	= AdminHelper::passwordEncode($_POST['old']);
    		$password	= AdminHelper::passwordEncode($_POST['password']);
    		$find	= ORM_Finder::findById('Admin', $_SESSION['adminid']);
    		
    		if($find->getPassword()==$old)
    		{
    			$find->setPassword($password);
    			$find->save();
    			$msg	= '更新成功!';
    		}else{
    			$msg	= '旧密码错误!';
    		}
    		$this->js($url, $msg);
    	}
    	$this->render();
    }
    
    public function doAdminLIst()
    {
    	$this->setPageTitle('管理员管理');
    	$list	= ORM_Finder::find('Admin');
    	$this->set('list', $list);
    	$this->render();
    }
    
    public function doAdminUpdate($id=0)
    {
    	$find	= ORM_Finder::findById('Admin', $id);
    	$ids	= 0;
    	$msg	= '更新成功';
    	if(empty($find))
    	{
    		$find	= new Admin();
    		$msg	= '添加成功';
    	}
    	
    	if($find->getIds()==0){
    		$ids	= 1;
    	}
    	
    	#更新操作
    	if(!empty($_POST) && $_SESSION['ids']==0)
    	{
    		$admin	=  $this->postVar('admin');
    		#是否添加超级管理员
    		if($admin==1 && $_SESSION['ids']==0){
    			$ids	= 0;
    		}else{
    			$ids	= implode(',', $this->postVar('ids'));
    		}
    		
    		$find->setUsername($this->postVar('username'));
    		$find->setPassword(AdminHelper::passwordEncode($this->postVar('password')));
    		$find->setRemark($this->postVar('remark'));
    		$find->setIds($ids);
    		$find->save();
    		$this->js(url_pre(), $msg);
    	}else{
    		$msg	= '权限不够!';
    	}
    	
    	$this->set('id', $id);
    	$this->set('ids', $ids);
    	$this->set('find', $find);
    	$this->render();
    }
    
    public function doAdminDelete($id)
    {
    	$find	= ORM_Finder::findById('Admin', $id);
    	if(!empty($find)){
    		$find->delete();
    		$msg	= '删除成功!';
    	}else{
    		$msg	= '删除失败!';
    	}
    	
    	$this->js(url_pre(), $msg);
    }
    
    public function doPoint()
    {
    	$this->view->layout = '';
    	$this->render();
    }
    
    public function doModule() {
        $m = camelize($this->params['m']);
        $a = camelize($this->params['a']);
        $module_class = $m.'Admin';
        
        $module = new $module_class;
        $params = $this->params;
        $params['module'] = $m;
        $params['action'] = $a;
        $module->setParams($params);
        $module->setView($this->view);
        $module->execute();
    }
    public function doLogin() {
        $this->setPageTitle('手机后台登陆管理');
        $this->render();
    }
}

